package
{
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	
	import spark.components.Image;
	import spark.components.Label;
	import spark.components.SkinnableContainer;
	import spark.layouts.VerticalLayout;

	public class ImageExplorerFishEyeContainer extends FishEyeContainer
	{
		public function ImageExplorerFishEyeContainer()
		{
			super();
			
			updateElements(File.desktopDirectory);
		}
		
		protected function updateElements(directory:File):void
		{
			var listing:Array = directory.getDirectoryListing();
			
			removeAllElements();
			
			if(directory.parent != null) {
				addElement(createImageContainer(directory.parent, true));
			}
			
			while(listing.length > 0) {
				var file:File = (listing.shift() as File);
				if(file.isDirectory || file.extension == 'jpg' || file.extension == 'gif' || file.extension == 'png') {
					var image:Image = new Image;
					if(file.isDirectory) {
						addElement(createImageContainer(file, false));
					} else {
						image.source = file.url;
//						image.load(file.url);
						addElement(image);
					}
				}
			}
		}
		
		protected function addEventListenerInDirectory(container:SkinnableContainer, file:File):void 
		{
			container.addEventListener(MouseEvent.CLICK,  function():void {
				updateElements(file);
			});
		}
		
		protected function createImageContainer(file:File, parent:Boolean):SkinnableContainer
		{
			var container:SkinnableContainer = new SkinnableContainer;
			[Embed(source='images/Folder.png')]
			var imageSource:Class
			var image:Image = new Image;
			var label:Label = new Label;
			var layout:VerticalLayout = new VerticalLayout;
			
			layout.horizontalAlign = 'center';
			image.source = imageSource;
			image.percentWidth = 100;
			image.percentHeight = 90;
			if(parent) {
				label.text = '../';
			}
			label.text += file.name;
			label.setStyle('color', '#FFFFFF');
			container.layout = layout;
			container.addElement(image);
			container.addElement(label);
			addEventListenerInDirectory(container, file);
			
			return container;
		}
	}
}